package com.winkyle.woodenfish.controller.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;

import com.winkyle.woodenfish.factory.presenterfactory.PresenterMvpFactory;
import com.winkyle.woodenfish.factory.presenterfactory.impl.PresenterMvpFactoryImpl;
import com.winkyle.woodenfish.mvpview.BaseMvpView;
import com.winkyle.woodenfish.presenter.BaseMvpPresenter;
import com.winkyle.woodenfish.proxy.PresenterProxy;
import com.winkyle.woodenfish.proxy.impl.BaseMvpProxy;
import com.winkyle.woodenfish.util.UserConfig;
import com.winkyle.woodenfish.widget.CustomDialog;
import com.winkyle.woodenfish.widget.CustomProgressDialog;

/**
 * @author winkyle
 * @date 2018/06/15
 * @description 继承自 {@link Fragment}的基类AbstractMvpFragment
 *               使用代理模式来代理Presenter的创建、销毁、绑定、解绑以及Presenter的
 *               状态保存,其实就是管理Presenter的生命周期
 */

public class AbstractMvpFragment<V extends BaseMvpView, P extends BaseMvpPresenter<V>> extends Fragment implements PresenterProxy<V,P>,BaseMvpView{
    private static final String PRESENTER_SAVE_KEY = "presenter_save_key";
    /**
     * 创建被代理对象,传入默认Presenter的工厂
     */
    private BaseMvpProxy<V,P> mProxy = new BaseMvpProxy<>(PresenterMvpFactoryImpl.<V,P>createFactory(getClass()));


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.onCreate(savedInstanceState);
        Log.e("perfect-mvp","V onCreate");
        Log.e("perfect-mvp","V onCreate mProxy = " + mProxy);
        Log.e("perfect-mvp","V onCreate this = " + this.hashCode());
        if(savedInstanceState != null){
            mProxy.onRestoreInstanceState(savedInstanceState.getBundle(PRESENTER_SAVE_KEY));
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e("perfect-mvp","V onResume");
        mProxy.onResume((V) this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("perfect-mvp","V onDestroy = " );
        mProxy.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.e("perfect-mvp","V onSaveInstanceState");
        outState.putBundle(PRESENTER_SAVE_KEY,mProxy.onSaveInstanceState());
    }

    @Override
    public void setPresenterFactory(PresenterMvpFactory<V, P> presenterFactory) {
        Log.e("perfect-mvp","V setPresenterFactory");
        mProxy.setPresenterFactory(presenterFactory);
    }
    @Override
    public PresenterMvpFactory<V, P> getPresenterFactory() {
        Log.e("perfect-mvp","V getPresenterFactory");
        return mProxy.getPresenterFactory();
    }
    @Override
    public P getMvpPresenter() {
        Log.e("perfect-mvp","V getMvpPresenter");
        return mProxy.getMvpPresenter();
    }

    @Override
    public void showRemindDialog(String msg) {
        new CustomDialog(getContext())
                .setTitleText("提示")
                .setContentText(msg)
                .setConfirmText("确定")
                .show();
    }


    /**
     * 进度条
     */
    private CustomProgressDialog customProgressDialog;


    @Override
    public void showProgress(){
        showProgress(null);
    }

    /**
     * @description 若进度条对话框为空,则创建进度条对话框，若信息不为空，
     *               则设置要显示的信息，若进度条对话框未显示则显示
     * @param msg 要显示的信息
     * @return
     */
    @Override
    public void showProgress(String msg){
        if(customProgressDialog == null){
            customProgressDialog = new CustomProgressDialog(getContext());
        }
        if(msg!=null){
            customProgressDialog.setMessage(msg);
        }
        if(!customProgressDialog.isShowing()){
            customProgressDialog.show();
        }
    }


    /**
     * @description 若进度条对话框不为空，且正显示着，则隐藏进度条
     * @return
     */
    @Override
    public void hideProgress(){
        if(customProgressDialog!=null&&customProgressDialog.isShowing()){
            customProgressDialog.dismiss();
        }
    }

    private Toast toast = null;

    /**
     * @description 封装弹出消息Toast
     * @param msg 消息
     * @param type
     * @return
     */
    public void showToast(final String msg, final int type){
        if (!TextUtils.isEmpty(msg)) {
            ((Activity)getContext()).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (toast == null) {
                        toast = Toast.makeText(getContext(), msg, type);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    } else {
                        toast.setText(msg);
                    }
                    toast.show();
                }
            });

        }
    }

    @Override
    public void showLongToast(String msg) {
        this.showToast(msg,Toast.LENGTH_LONG);
    }


    @Override
    public void showShortToast(String msg) {
        this.showToast(msg, Toast.LENGTH_SHORT);
    }

    @Override
    public void startActivity(Class<? extends Activity> clazz) {
        this.startActivity(clazz,new String[0]);
    }

    @Override
    public void startActivity(Class<? extends Activity> clazz, String... data) {
        Intent intent = new Intent();
        intent.setClass(getContext(), clazz);
        if(data!=null){
            Log.d("data",":null");
        }
        if(data!=null&&data.length % 2 != 1) {
            for (int i = 0; i < data.length / 2; i++) {
                intent.putExtra(data[i * 2], data[i * 2 + 1]);
            }
        }
        startActivity(intent);
    }

    /**
     * @description 是否登录状态
     * @return
     */
    protected boolean isLogin(){
        return getContext()!=null&&UserConfig.getInstance(getContext()).getCookie()!=null;
    }
}
